/**
 * 
 */
package ar.edu.unq.svnpermission.panels;

import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import ar.edu.unq.svnpermission.components.AutoAttacheablePanel;
import ar.edu.unq.svnpermission.domain.Folder;
import ar.edu.unq.svnpermission.ui.model.FolderDetail;
import ar.edu.unq.svnpermission.ui.model.Members;
import ar.edu.unq.svnpermission.ui.model.Permissions;

/**
 * Panel para la creaciòn de Permisos
 * @author nicolas
 */
public class PermissionPanel extends AutoAttacheablePanel{
	private static final long serialVersionUID = 1L;
	private Folder folder;
	private ModalWindow modalWindow;

	public PermissionPanel(String id,MarkupContainer container) {
		super(id,container);
		this.buildPanel();
	}

	public PermissionPanel(String id, Component parent, final ModalWindow window, IModel<?> model) {
		super(id,(MarkupContainer)parent,model);
		this.setFolder(((Permissions)this.getDefaultModelObject()).getFolder());
		this.setModalWindow(window);
		this.buildPanel();
	}	
	
	
	/**
	 * Se encarga de construir el panel
	 */
	protected void buildPanel()
	{
		Form<Permissions> form = new Form<Permissions>("permissionForm", this.createModel());
		this.addCheckBoxes(form);
		this.addMembershipDropDown(form);
		this.addButtons(form);
		this.add(form);
	}
	

	
	/**
	 * Se encarga de agregar los campos al form
	 * @param form
	 * @author nicolas
	 */
	@SuppressWarnings("rawtypes")
	protected void addCheckBoxes(Form form)
	{
		form.add(new CheckBox("hasReadAccess"));
		form.add(new CheckBox("hasWriteAccess"));		
	}
	
	/**
	 * Se encarga de agregar un Drop Down con los members
	 * @param form
	 * @author nicolas
	 */
	@SuppressWarnings("rawtypes") 
	protected void addMembershipDropDown(Form form)
	{
		DropDownChoice members = new DropDownChoice("who",new Members().getAll());
		form.add(members);
	}
	
	/**
	 * Crea el modelo del formulario que sera' el caso de uso.
	 */
	protected CompoundPropertyModel<Permissions> createModel() {
		return new CompoundPropertyModel<Permissions>((Permissions)this.getDefaultModelObject());
	}	
	
	/**
	 * Se encarga de agregar los botones
	 * @param form
	 * @author nicolas
	 */
	@SuppressWarnings("rawtypes") 
	protected void addButtons(Form form)
	{
		form.add(new AjaxSubmitLink("create") {

			private static final long serialVersionUID = 1L;


			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				((Permissions)form.getModelObject()).create();
				PermissionPanel.this.getModalWindow().close(target);	
				
			}

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				
			}


		});		
	}
	
	
	public Folder getFolder() {
		return folder;
	}

	public void setFolder(Folder folder) {
		this.folder = folder;
	}

	public void setModalWindow(ModalWindow modalWindow) {
		this.modalWindow = modalWindow;
	}

	public ModalWindow getModalWindow() {
		return modalWindow;
	}

	@Override
	public void build() {
		// TODO Auto-generated method stub
		
	}




}
